import router from './router'
import store from './store'
const whileList = ['/login', '/404']
// 花裤衩自己封装了函数用来显示网页的标题
import getPageTitle from '@/utils/get-page-title'
router.beforeEach(async(to, from, next) => {
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      // 从meta里面获取当前页面的title标题
      document.title = getPageTitle(to.meta.title)
      // 判断vuex中的头肯是否过期,如果存进去了,如果村进去了就不在获取,也是优化路由守卫,减少获取信息的次数
      // if (!store.state.user.userIfon.userId) {
      await store.dispatch('user/getUserInfo')
      // }
      next()
    }
  } else {
    if (whileList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
